
require 'singleton'
class ResponderChain
	include Singleton
	def initialize
		@responders=[]
	end
	def add_responder(responder)
		@responders<<responder
	end
	def remove_responder(responder)
		@responders.delete(responder)
	end
	def delegate(name, *args)
		@responders.each do |responder| 
			if responder.respond_to?(name)
				responder.send(name, *args)
				break
			end
		end
	end
end
class NilClass
	def method_missing(name, *args)
		ResponderChain.instance.delegate(name, *args)
	end
end


#class Foo
#	def fooz(name)
#		puts "hello #{name} from Foo#fooz"
#	end
#end

#class Bar	
#	def initialize(number)
#		@number=number
#	end
#	def baz(name)
#		puts "hello #{name} from Bar#baz #{@number}"
#	end
#end

##
#bar0=Bar.new(0)
#ResponderChain.instance.add_responder(bar0)
#ResponderChain.instance.add_responder(Foo.new)
#ResponderChain.instance.add_responder(Foo.new)
#ResponderChain.instance.add_responder(Bar.new(1))
#ResponderChain.instance.add_responder(Bar.new(2))
#ResponderChain.instance.add_responder(Bar.new(3))
#
#nil.fooz("aaron")
#nil.baz("aaron")
#ResponderChain.instance.remove_responder(bar0)
#puts "removing bar0"
#nil.fooz("aaron")
#nil.baz("aaron")




